package cn.edu.zzuli.common.utils;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;

/**
 *
 * @Description WeChatUtil工具
 */
@Slf4j
public class WeChatUtil {

    //发送订阅消息
    public static final String MESSAGE_SUBSCRIBE_SEND = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESSTOKEN";



    /**
     * 发送
     *
     * @param accessToken
     * @param openId
     * @param templateId
     * @param json
     * @return
     */
    public static String messageSubscribeSend(String accessToken, String openId, String templateId, JSONObject json) {
        JSONObject body = new JSONObject();
        body.set("touser", openId);//接受人的openId
        body.set("template_id", templateId);//模版ID
        body.set("data", json);//如餐
        String requestUrl = MESSAGE_SUBSCRIBE_SEND.replace("ACCESSTOKEN", accessToken);
        String post = HttpUtil.post(requestUrl, body.toString());
        return post;
    }


    /**
     *
     * @param templateId
     * @param openId
     * @param json
     */
    public static void wxSend(String templateId,String openId,JSONObject json) {
        String accessToken = AccessToken.getAccessToken();
        String res = messageSubscribeSend(accessToken, openId, templateId, json);
        System.out.println(res);
    }

}